<?php

include_once ("./libs/bn_path.php");
$gf = new bn_genralfunc;
ob_start();

if (!empty($_POST['install']))
{
    $server = $_POST['server'];
    $db = $_POST['database'];
    $user = $_POST['user'];
    $pass = $_POST['pass'];


    $link = mysql_connect($server, $user, $pass);
    if (!$link)
    {
        $msg = 'Could not connect: ' . mysql_error();
        $gf->msgbox($msg, 4, 'خطا');
    }
    mysql_query("CREATE DATABASE IF NOT EXISTS `$db` DEFAULT CHARACTER SET utf8 COLLATE utf8_persian_ci",
        $link);
    file_put_contents('./libs/info.php', "
        <?php
           define('host','$server') ;
           define('db_name','$db') ;
           define('user','$user') ;
           define('pass','$pass') ;
        ?>");
    $db = new bn_database;
    $db->reset_database();

    $msg = 'install database success';
    $gf->msgbox('بانک با موفقیت ایجاد شد', 4, 'اطلاعات');
}

if (!empty($_POST['save']))
{
    $dl = new bn_dal;

    $user = $_POST['usera'];
    $pass = $_POST['passa'];
    $repass = $_POST['repass'];
    $mail = $_POST['mail'];
    $tzone = $_POST['tzone'];
    $lang = $_POST['lang'];

    date_default_timezone_set($tzone);
    if ($pass != $repass)
        $gf->msgbox("pass:$pass<br>repass:$repass", 3, 'خطا');
    if (strlen($pass) < 5)
        $gf->msgbox('طول کلمه عبور کوتاه میباشد', 3, 'خطا');
    $pass = $gf->Hash_string($pass);
    $date = date('Y-m-d h:i:s', strtotime('now'));

    $dl->update('config', array('timezone' => $tzone), array('name' => 'timezone'));
    $user = bn_user;
    $user->update($user, $pass, '', '', $mail, 1);
    unset($user);
    $msg = 'اطلاعات کاربری با موفقیت ذخیره شد';
    $gf->msgbox($msg, 4, 'اطلاعات');
    bn_redirect('/admin/login.php');
}

?>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="content-type" content="text/html" />
	<meta name="author" content="salam" />
    <link rel="stylesheet" href="./themplate/css/bn_maincss.css" />

	<title>install</title>
</head>

<body>
<form method="post">

<table style="margin: 0 auto;<?php

if (file_exists('./libs/info.php'))
{
    $dl = new bn_dal;
    if (count($dl->Tables()) == 12)
        echo 'display:none;';
}

?>" class="table">
<tr>
	<td><input type="text" size="25" autocomplete="on" name="server" class="input_text" /></td>
    <td>نام سرور</td>
</tr>
<tr>
	<td><input type="text" size="25" autocomplete="on" name="user" class="input_text" /></td>
    <td>mysqlنام کاربری </td>
</tr>
<tr>
	<td><input type="text" size="25" autocomplete="on" name="pass" class="input_text" /></td>
    <td>mysqlکلمه عبور </td>
</tr>
<tr>
	<td><input type="text" size="25" autocomplete="on" name="database" class="input_text" /></td>
    <td>نام بانک</td>
</tr>
<tr>
	<td colspan="2"><input type="submit" value="نصب بانک" name="install" class="button" /></td>
</tr>
</table>
</form>
<form method="post">
<table style="margin: 0 auto;<?php

if (!file_exists('./libs/info.php'))
    echo 'display:none;';

unset($dl);

?>" class="table">
<tr>
	<td><input type="text" size="25" autocomplete="on" name="usera" class="input_text" /></td>
	<td>نام کاربری</td>
</tr>
<tr>
	<td><input type="text" size="25" autocomplete="on" name="passa" class="input_text" /></td>
	<td>رمز عبور</td>
</tr>
<tr>
	<td><input type="text" size="25" autocomplete="on" name="repass" class="input_text" /></td>
	<td>رمز عبور</td>
</tr>
<tr>
	<td><input type="text" size="30" autocomplete="on" name="mail" class="input_text" /></td>
	<td>آدرس پست الکترونیک</td>
</tr>
<tr>
	<td><input type="text" size="30" autocomplete="on" name="tzone" class="input_text" /></td>
	<td>تنظیم وقت محلی</td>
</tr>
<tr>
	<td><input type="text" size="30" autocomplete="on" name="lang" class="input_text" /></td>
	<td>زبان پیشفرض</td>
</tr>
<tr>
    <td colspan="2"><input type="submit" value="ثبت" name="save" class="button" /></td>
</tr>
</table>
</form>
</body>
</html>